﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardInChina.Bussiness.Frontend.Interface;
using CardInChina.Bussiness.Frontend.Models;
using CardInChina.Data.Entity.Entity;

namespace CardInChina.Web.Mvc.Controllers
{
    public class CustomerController : CICBasicController
    {
        private static string[] orderTypes = { "tobepaid", "paid", "cancelled" };
        private UserService us = new UserService();
        //
        // GET: /Customer/

        [SessionAuthorize]
        public ActionResult Welcome()
        {
            CustomerOrderInfoInUserWelcome customer = us.GetCustomerOrderInfoInUserWelcomeByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customer;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Profile()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Save()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Recommend()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Favorite(int? page)
        {
            if (page == null)
                ViewBag.Page = 1;
            else
                ViewBag.Page = (int)page;
            var favorites = us.GetCustomerFavoriteInfoInUserWelcomeByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = favorites;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Bonus()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Deposit()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Points()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }


        [SessionAuthorize]
        public ActionResult Delegation()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Remittance()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Alipay()
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }

        [SessionAuthorize]
        public ActionResult Order(string ordertype)
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            if (string.IsNullOrEmpty(ordertype)) return new HttpNotFoundResult();
            if (!orderTypes.Contains(ordertype)) return new HttpNotFoundResult();
            ViewBag.OrderType = ordertype;
            return View();
        }

        [SessionAuthorize]
        public ActionResult OrderDetail(string orderid)
        {
            Customer customerBasicInfo = us.GetCustomerBasicInfoByCustomerID(CustomerInfo.CustomerID);
            ViewData.Model = customerBasicInfo;
            return View();
        }


        public ActionResult LogIn()
        {
            //view with layout2 should tell ViewBag.Title
            ViewBag.Title = "用户中心";
            ViewBag.ReturnUrl = HttpContext.Request.QueryString["ReturnUrl"];
            return View();
        }

        /// <summary>
        /// Logout
        /// </summary>
        public ActionResult LogOut()
        {
            CurrentUser.Details = null;
            return Redirect("/");
        }

        [HttpPost]
        public ActionResult LogIn(UserLoginInfo uli)
        {
            if (uli.validCode != null && uli.validCode == CurrentUser.HipCode)
            {
                var userInfo = us.ValidUserLogIn(uli);
                if (userInfo != null)
                {
                    CurrentUser.Details = userInfo;
                }
                else
                {
                    CurrentUser.Details = null;
                }
            }
            return Redirect(uli.ReturnUrl);
        }

        public ViewResult Register()
        {
            if (CurrentUser.Recommended != null)
            {
                var recommended = us.GetCustomerBasicInfoByCustomerID(CurrentUser.Recommended.Value);
                ViewBag.Recommended = recommended.GetDisplayName();
            }
            if (CurrentUser.Referrer != null)
            {
                ViewBag.Referrer = CurrentUser.Referrer;
            }
            return View();
        }

        [SessionAuthorize(RequireEmailVerify = false)]
        public ViewResult RegisterComplete()
        {
            return View();
        }

        [SessionAuthorize(RequireEmailVerify = false)]
        public ViewResult EmailNotVerified()
        {
            return View();
        }

        [SessionAuthorize]
        public ViewResult RegisterSucceed()
        {
            return View();
        }

        public ActionResult RegisterVerify(string code)
        {
            var cc = us.VerifyEmail(code);

            if (cc != null)
            {
                CurrentUser.Details = cc;
                return Redirect("/user/register-succeed");
            }
            else
            {
                return Redirect("/");
            }
        }

        [HttpPost]
        public ActionResult Register(FormCollection form)
        {
            var referrer = CurrentUser.Referrer;
            var recommended = CurrentUser.Recommended;
            var email = form["reg.email"];
            var alias = form["reg.alias"];
            var password = form["reg.password"];
            var hip = form["reg.hip"];
            var ip = Request.UserHostAddress;

            if (hip != CurrentUser.HipCode)
                throw new ArgumentException("Invalid hip code");

            var customer = us.Register(email, alias, password, referrer, recommended, ip);
            CurrentUser.Details = customer;

            return Redirect("/user/register-complete");
        }

        public ViewResult ForgotPwd()
        {
            return View();
        }


        [SessionAuthorize]
        public ViewResult Activate()
        {
            return View();
        }

        [HttpPost]
        [SessionAuthorize]
        public ActionResult Activate(FormCollection collection)
        {
            return Redirect("/user");
        }


        [SessionAuthorize]
        public ActionResult Verify()
        {
            return View();
        }

        [HttpPost]
        [SessionAuthorize]
        public ActionResult Verify(FormCollection collection)
        {
            return Redirect("/user");
        }

        [SessionAuthorize]
        public ActionResult Promote()
        {
            return View();
        }

        [HttpPost]
        [SessionAuthorize]
        public ActionResult Promote(FormCollection collection)
        {
            return Redirect("/user");
        }


        [SessionAuthorize]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        [SessionAuthorize]
        public ActionResult ChangePassword(FormCollection collection)
        {
            return Redirect("/user/profile");
        }


    }
}
